package com.basic.java.threads.sleep;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 模拟计时类
 * 创建时间：2022/4/18 22:44
 *
 * @author gejin
 */
public class TestSleep2 {
    public static void main(String[] args) throws InterruptedException {
        //testDown();
        Date date = new Date();
        printTime(date);
    }

    // 隔一秒打印当前时间
    private static void printTime(Date date) throws InterruptedException {
        while (1 == 1) {
            Thread.sleep(1000);
            String format = new SimpleDateFormat("HH:mm:ss").format(date);
            date = new Date(System.currentTimeMillis());
            System.out.println(format);
        }
    }

    // 模拟倒计时
    static void testDown() throws InterruptedException {
        int num = 10;
        while (true) {
            System.out.println(num);
            Thread.sleep(1000);
            if (num <= 0) {
                break;
            }
            num--;
        }
    }
}

